package com.crm.common.config;

import com.crm.common.handler.LoginInterceptor;
import lombok.AllArgsConstructor;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.util.ArrayList;
import java.util.List;

@Configuration
//@AllArgsConstructor
public class WebConfig implements WebMvcConfigurer {

	@Value("${file.upload-dir}")
	private String uploadDir;

	private final LoginInterceptor loginInterceptor;

	// 手动创建构造函数，只包含需要的参数
	public WebConfig(LoginInterceptor loginInterceptor) {
		this.loginInterceptor = loginInterceptor;
	}

	@Override
	public void addResourceHandlers(ResourceHandlerRegistry registry) {
		// 将物理路径映射为虚拟URL路径
		registry.addResourceHandler("/contract/**")
				.addResourceLocations("file:" + uploadDir + "/");
	}

	@Override
	public void addInterceptors(InterceptorRegistry registry) {
		// 配置放行资源
		// 无需拦截的接口集合
		List<String> ignorePath = new ArrayList<>();
		// knife4j(swagger)
		ignorePath.add("/swagger-resources/**");
		ignorePath.add("/swagger-ui/**");
		ignorePath.add("/doc.html");
		ignorePath.add("/function/**");
		ignorePath.add("/v3/**");
		ignorePath.add("/webjars/**");
		ignorePath.add("/static/**");
		ignorePath.add("/templates/**");
		ignorePath.add("/error");
		// 登录页面
		ignorePath.add("/user/login");
		registry.addInterceptor(loginInterceptor).excludePathPatterns(ignorePath);
	}
/*	@Override
	public void addResourceHandlers(ResourceHandlerRegistry registry) {
		// 添加对上传文件目录的静态资源映射
		registry.addResourceHandler("/contract/**")
				.addResourceLocations("file:./src/main/resources/static/contract/");
	}*/
}

